//
//  TransactionStore.m
//  NewPro
//
//  Created by RYX on 2018/11/24.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "TransactionStore.h"
#import "MerchantModel.h"
#import "TradeDetailModel.h"

@implementation TransactionStore

#pragma mark - 交易明细列表
- (void)getMerchantListWithCustomId:(NSString *)customId Success:(void(^)(NSArray *listArr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/tradeDetail/merchantList",IP];
    NSDictionary *dic = @{
                          @"customId":customId
                          };
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            NSArray *arr = [MerchantModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"items"]];
            success(arr);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}


#pragma mark - 明细详情
- (void)getTradeDetailListWithMerchantNo:(NSString *)merchantNo andMonth:(NSString *)month Success:(void(^)(NSArray *listArr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/tradeDetail/tradeDetailList",IP];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    if (merchantNo.length > 0) {
        
        [dic setObject:merchantNo forKey:@"merchantNo"];
    }
    
    if (month.length > 0) {
        
        [dic setObject:month forKey:@"month"];
    }
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            NSArray *arr = [TradeDetailModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"items"]];
            
            
            for (TradeDetailModel *model in arr) {
                
                model.businessTime = [StrTool changeWithTime:model.businessTime];
            }
            NSMutableArray *array = [NSMutableArray arrayWithArray:arr];
        
            NSMutableArray *dateMutablearray = [@[] mutableCopy];
            
            for (int i = 0; i < array.count; i ++) {
                
                TradeDetailModel *model = array[i];
                
                NSMutableArray *tempArray = [@[] mutableCopy];
                
                [tempArray addObject:model];
                
                for (int j = i+1; j < array.count; j ++) {
                    
                    TradeDetailModel *model2 = array[j];
                    
                    if([[model.businessTime substringToIndex:10] isEqualToString:[model2.businessTime substringToIndex:10]]){
                        
                        [tempArray addObject:model2];
                        
                        [array removeObjectAtIndex:j];
                        
                        j -= 1;
                        
                    }
                    
                }
                
                [dateMutablearray addObject:tempArray];
                
            }
            
            NSLog(@"dateMutable:%@",dateMutablearray);
            success(dateMutablearray);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}

#pragma mark - 明细详情查询
- (void)getTradeDetailListWithMerchantNo:(NSString *)merchantNo andDetailTime:(NSString *)time Success:(void(^)(NSArray *listArr))success Failure:(void(^)(NSError *error))failure{
    
    NSString *url = [NSString stringWithFormat:@"%@/api/tradeDetail/tradeDetailListByDay",IP];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    if (merchantNo.length > 0) {
        
        [dic setObject:merchantNo forKey:@"merchantNo"];
    }
    
    if (time.length > 0) {
        
        [dic setObject:time forKey:@"month"];
    }
    
    [HttpTool postUrlWithString:url parameters:dic success:^(id responseObject) {
        
        NSError *error = [HttpTool inspectError:responseObject];
        
        if (error == nil) {
            
            NSArray *arr = [TradeDetailModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"items"]];
            
            
            for (TradeDetailModel *model in arr) {
                
                model.businessTime = [StrTool changeWithTime:model.businessTime];
            }
            NSMutableArray *array = [NSMutableArray arrayWithArray:arr];
            
            NSMutableArray *dateMutablearray = [@[] mutableCopy];
            
            for (int i = 0; i < array.count; i ++) {
                
                TradeDetailModel *model = array[i];
                
                NSMutableArray *tempArray = [@[] mutableCopy];
                
                [tempArray addObject:model];
                
                for (int j = i+1; j < array.count; j ++) {
                    
                    TradeDetailModel *model2 = array[j];
                    
                    if([[model.businessTime substringToIndex:10] isEqualToString:[model2.businessTime substringToIndex:10]]){
                        
                        [tempArray addObject:model2];
                        
                        [array removeObjectAtIndex:j];
                        
                        j -= 1;
                        
                    }
                    
                }
                
                [dateMutablearray addObject:tempArray];
                
            }
            
            NSLog(@"dateMutable:%@",dateMutablearray);
            success(dateMutablearray);
        }else{
            
            failure(error);
        }
        
    } failure:^(NSError *error) {
        
        failure(error);
    }];
    
}




@end
